/*
 * 文件名: MenuDialog.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XIAOFANQING001
 * 创建时间: 2012-1-16
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package com.longevityassistant.menu;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.longevityassistant.activity.R;

/**
 * 用户按物理Menu键弹出的Menu对话框对象
 * @author EX-XIAOFANQING001
 * @date 2012-1-16
 * @version [Android PABank C01, @2012-1-16]
 * @description
 */
public class MenuDialog extends Dialog implements OnItemClickListener{
    
    private List<MenuItem> mMenuItems;
    
    private GridView gvMenus;

    public MenuDialog(Context context)
    {
        super(context, android.R.style.Theme_InputMethod);
        super.setContentView(R.layout.app_widget_menu_view);
        gvMenus = (GridView) findViewById(R.id.gv_menus);
        gvMenus.setOnItemClickListener(this);
        
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    
    public MenuDialog(Context context, List<MenuItem> menuItems) {
        this(context);
        setMenus(menuItems);
    }
    
    /**
     * 设置Menu数据
     * @param menuItems
     */
    public void setMenus(List<MenuItem> menuItems) {
        mMenuItems = menuItems;
        MenuAdapter menuAdapter = new MenuAdapter();
        gvMenus.setAdapter(menuAdapter);
    }
    
    /**
     * 用户触碰屏幕的任意位置Menu对话框都消失 
     * @param event
     * @return
     * @see android.app.Dialog#onTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        dismiss();
        return super.onTouchEvent(event);
    }
    
    /**
     * 当Menu对话框显示的时候,用户按了Menu 物理键 Menu对话框消失
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        MenuItem mi = mMenuItems.get(position);
        if (mi.getmOnMenuClickListener() != null) {
            mi.getmOnMenuClickListener().onClick(view);
        }
        dismiss();
    }

    /**
     * Menu九宫格的Adapter对象 
     * @author EX-XIAOFANQING001
     * @version [Android PABank C01, 2012-1-13]
     */
    private class MenuAdapter extends BaseAdapter {
        private ViewHolder holder;
        
        @Override
        public int getCount()
        {
            if (mMenuItems != null) {
                return mMenuItems.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position)
        {
            if (mMenuItems != null) {
                return mMenuItems.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.app_widget_menu_view_cell, null);
                
                holder = new ViewHolder();
                holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
                holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            MenuItem menuItem = mMenuItems.get(position);
            
            holder.tvTitle.setText(menuItem.getTitle());
            holder.ivIcon.setImageResource(menuItem.getIcon());
            
            return convertView;
        }
        
        class ViewHolder{
            TextView tvTitle;
            ImageView ivIcon;
        }
    }
}

